<?php
namespace app\share\controller;

use app\common\controller\Base;

class Captcha extends Base
{
    private $result = [
        'msg'  => '验证码发送成功',
        'code' => 1,
        'data' => [],
    ];
    
    //发送短信验证码
    public function index()
    {
        $mobile  = input('post.mobile/s');
        $captcha = input('post.captcha/s');
        if(!$mobile){
             $this->result['msg']  = '请输入手机号';
             $this->result['code'] = -1;
             return json($this->result);
        }
        if(!captcha_check($captcha)){
             $this->result['msg']  = '安全码错误';
             $this->result['code'] = -1;
             return json($this->result);
        };
        //发送验证码
        $model = model('common/Captcha','loglic');
        if(!$model->send($mobile)){
             $this->result['msg']  = $model->getError();
             $this->result['code'] = -2;
             return json($this->result);
        }
        //返回发送结果
        return json($this->result);
    }

    //图形验证码
    public function image()
    {
        $captcha = new \think\captcha\Captcha([
            'length'   => 4, 
            'fontSize' => 48,    
            'useNoise' => true, 
            'useCurve' => false,
            'codeSet'  => '123456789',
        ]);
        return $captcha->entry();
    }
}